<template>
  <a-drawer :title="bom.Name" width="80%" :bordered="false" placement="right" :closable="false" :visible="visible" @close="onClose">
    <div class="table-page-search-wrapper">
      <a-descriptions bordered>
        <a-descriptions-item label="产品" :span="2">
          <CommonSelect v-model="bom.ProductId" table="MaterielInfor" disabled></CommonSelect>
        </a-descriptions-item>
        <a-descriptions-item label="产品规格" :span="2">
          <CommonSelect v-model="bom.SpecId" table="MaterielSpec" disabled></CommonSelect>
        </a-descriptions-item>
        <a-descriptions-item label="BOM编码" :span="2">
          {{ bom.Code }}
        </a-descriptions-item>
        <a-descriptions-item label="BOM名称" :span="2">
          {{ bom.Name }}
        </a-descriptions-item>
        <a-descriptions-item label="电气编码" :span="2">
          {{ bom.EleCode }}
        </a-descriptions-item>
      </a-descriptions>
    </div>
    <a-card title="BOM清单" style="width: 100%">
      <div class="table-operator">
        <a-button type="primary" v-action:Add icon="plus" @click="handleAdd">新建</a-button>
      </div>

      <a-table ref="table" v-if="data.length" size="default" :indentSize="55" bordered rowKey="Id" :columns="columns" defaultExpandAllRows="true" :dataSource="data" childrenColumnName="Children" :pagination="false">
        <span slot="action" slot-scope="text, record">
          <template>
            <a v-action:Update @click="handleAddChild(record.Id)">加子级</a>
            <a-divider type="vertical" />
            <a v-action:Update @click="handleAddChild(record.ParentId)">加同级</a>
            <a-divider type="vertical" />
            <a v-action:Update @click="handleEdit(record)">修改</a>
            <a-divider type="vertical" />
            <a v-action:Delete @click="handleDelete([record])">删除</a>
          </template>
        </span>
      </a-table>
      <EditForm ref="editForm" @Success="()=>{this.loadData()}"></EditForm>
    </a-card>
  </a-drawer>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import MainSvc from '@/api/Define/MaterialBomTreeSvc'
import EditForm from './Edit'
import EnumSelect from '@/components/CF/EnumSelect'
import EnumName from '@/components/CF/EnumName'
import CommonSelect from '@/components/CF/CommonSelect'

const columns = [
  { title: '组成物料', dataIndex: 'Materiel.Name', sorter: true },
  { title: '物料规格', dataIndex: 'Spec.Name', sorter: true },
  { title: '电气编码', dataIndex: 'EleCode', sorter: true },
  { title: '数量', dataIndex: 'Quantity', sorter: true },
  { title: '操作', dataIndex: 'action', width: '240px', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {
    STable,
    MainSvc,
    EnumSelect,
    EnumName,
    EditForm,
    CommonSelect
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      actionVisible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      data: [],
      selectedRowKeys: [],
      selectedRows: [],
      bom: {}
    }
  },
  filters: {
  },
  created() {},
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    moment,
    openDrawer(bom) {
        this.visible = true
        this.bom = bom
        this.loadData()
    },
    loadData() {
      MainSvc.GetTree(this.bom.Id).then(result => {
        if (result.Data) this.data = result.Data
      })
    },
    onClose() {
        this.visible = false
    },
    handleAdd() {
      this.mdl = null
      this.visible = true
      this.$refs.editForm.openForm(null, null, this.bom.Id, '新增')
    },
    handleAddChild(pid) {
      this.mdl = null
      this.visible = true
      this.$refs.editForm.openForm(pid, null, this.bom.Id, '新增')
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
      this.$refs.editForm.openForm(record.ParentId, record.Id, this.bom.Id, '修改')
    },
    resetSearchForm() {
      this.queryParam = { Keyword: '' }
    },
    handleDelete(rows) {
      var thisObj = this
      var ids = rows.map(value => value.Id)
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            MainSvc.Delete(ids).then(result => {
              resolve()
              if (result.Success) {
                thisObj.$message.success('操作成功!')
                thisObj.$refs.table.refresh()
              } else {
                thisObj.$message.error(result.Msg)
              }
            })
          })
        }
      })
    }
  }
}
</script>
